# -*- coding: utf8 -*-

import sys
import os
import os.path
import json

def getContent(line, flag):
    pos = line.find(flag)
    if pos == -1:
        return ""
    value = line[pos+len(flag)+1: len(line)-1]
    return value

class citem():
    def __init__(self):
        self.classname = ""
        self.classdesc = ""
        self.funcs = []

    def isnull(self):
        if self.classname == "" or self.classdesc == "":
            return True
        return False

    def setname(self, v):
        self.classname = v

    def setdesc(self, v):
        self.classdesc = v

    def addfuncs(self, f):
        self.funcs.append(f)

class item():
    def __init__(self):
        self.classn = ""
        self.func = ""
        self.desc = ""
        self.parm = []
        self.ret = ""

    def isnull(self):
        if self.classn == "" or self.func == "":
            return True
        return False

    def setclass(self, v):
        self.classn = v

    def setfunc(self, v):
        self.func = v

    def setdesc(self, v):
        self.desc = v

    def addparm(self, v):
        self.parm.append(v)

    def setret(self, v):
        self.ret = v

citemlist = []
itemlist = []

for parent,dirnames,filenames in os.walk("./src/client"):
    for filename in filenames:
        if filename.find(".h") != -1:
            if parent == "./src/client" and not filename.find(".h") == -1:
                filename = "./src/client/" + filename
                print filename
                fp = open(filename, 'r')
                begin = False
                line = fp.readline()
                v = item()
                c = citem()
                while line:
                    line = fp.readline();
                    if not begin and not line.find("/*") == -1:
                        begin = True
                    if begin and not line.find("*/") == -1:
                        if not v.isnull():
                            itemlist.append(v)
                            v = item()
                        if not c.isnull():
                            citemlist.append(c)
                            c = citem()
                        begin = False
                        
                    if begin and not line == "":
                        value = getContent(line, "@classname")
                        if not value == "":
                            c.setname(value)
                        value = getContent(line, "@classdesc")
                        if not value == "":
                            c.setdesc(value)
                        value = getContent(line, "@class")
                        if not value == "":
                            v.setclass(value)
                        value = getContent(line, "@desc")
                        if not value == "":
                            v.setdesc(value)
                        value = getContent(line, "@parm")
                        if not value == "":
                            v.addparm(value)
                        value = getContent(line, "@return")
                        if not value == "":
                            v.setret(value)
                        value = getContent(line, "@func")
                        if not value == "":
                            v.setfunc(value)
                fp.close()

infos = []
for c in citemlist:
    ci = {"classname":c.classname, "classdesc":c.classdesc, "funcs":[]}
    for i in itemlist:
        if i.classn == c.classname:
            ii = {"classn":i.classn, "func":i.func, "desc":i.desc, "parm":[], "return":i.ret}
            for p in i.parm:
                ii["parm"].append(p)
            ci["funcs"].append(ii)
    infos.append(ci)

jsonstr = "[\n"
for it in infos:
    jsonstr += "  {\n"
    jsonstr += "    \"classname\":\"" + it["classname"] + "\",\n"
    jsonstr += "    \"classdesc\":\"" + it["classdesc"] + "\",\n"
    jsonstr += "    \"funcs\":" + "[\n"
    for f in it["funcs"]:
        jsonstr += "     {\n"
        jsonstr += "      \"func\": \"" + f["func"] + "\",\n"
        jsonstr += "      \"desc\": \"" + f["desc"] + "\",\n"
        jsonstr += "      \"parm\": [\n"
        for p in f["parm"]:
            pn = p.split(" ")[0]
            pv = p[len(pn)+1:len(p)]
            jsonstr += "       {"
            jsonstr += "\"name\": \"" + pn + "\", \"desc\": \"" + pv + "\""
            if p == f["parm"][len(f["parm"])-1]:
                jsonstr += "}\n"
            else:
                jsonstr += "},\n"
        jsonstr += "      ],\n"
        jsonstr += "      \"return\": \"" + f["return"] + "\"\n"
        if f == it["funcs"][len(it["funcs"])-1]:
            jsonstr += "     }\n"
        else:
            jsonstr += "     },\n"
    jsonstr += "    ]\n"
    if it == infos[len(infos)-1]:
        jsonstr += "  }\n"
    else:
        jsonstr += "  },\n"
jsonstr += "]\n"

fp = open("src/node_maintain/app/api.json", "w")
fp.write(jsonstr)
fp.close()

